package com.orderplus.opshop.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.orderplus.opshop.common.util.date.DateStyle;
import com.orderplus.opshop.common.util.date.DateUtil;

/**
 * Title: 用于处理数组的工具类
 * Description: 用于处理数组的工具类
 * Company: Copyright @ 2019 OrderPlus 版权所有
 *
 * @version 1.0 初稿
 * @author: mhj
 * @date: 2012-11-2
 */
public class ArrayUtil {
	/**
	 * 将字符串数组转为以,号逗号隔开的字符串
	 *
	 * @param strArr
	 * @return
	 */
	public static String getStringFormArr(String[] strArr) {
		if (strArr == null) {
			return "";
		} else {
			StringBuffer resultsb = new StringBuffer();
			for (String content : strArr) {
				if (!ValueUtil.isEmpty(content)) {
					if (resultsb.length() > 0) {
						resultsb.append(",");
					}
					resultsb.append(content);
				}
			}

			if (resultsb.length() > 1) {
				resultsb.substring(0, resultsb.length() - 1);
			}

			return resultsb.toString();
		}
	}

	public static String getStringFormCollection(Collection<String> collection) {
		return getStringFormCollection(collection, ",");
	}

	public static String getStringFormCollection(Collection<String> collection, String sign) {
		if (collection == null) {
			return "";
		} else {
			StringBuffer resultsb = new StringBuffer();
			for (String content : collection) {
				if (!ValueUtil.isEmpty(content)) {
					if (resultsb.length() > 0) {
						resultsb.append(sign);
					}
					resultsb.append(content);
				}
			}
			return resultsb.toString();
		}
	}

	public static String getStringCollection(Collection<String> collection) {
		return getStringCollection(collection, ",");
	}

	public static String getStringCollection(Collection<String> collection, String sign) {
		if (collection == null) {
			return "";
		} else {
			StringBuffer resultsb = new StringBuffer();
			for (String content : collection) {
				if (!ValueUtil.isEmpty(content)) {
					if (resultsb.length() > 0) {
						resultsb.append(sign);
					}
					resultsb.append("'" + content + "'");
				}
			}
			return resultsb.toString();
		}
	}

	/**
	 * 判断字符串是否存在办理者
	 *
	 * @param transactors 以逗号隔开的用户列表
	 * @return
	 */
	public static boolean existTransactor(String transactors) {
		boolean result = false;
		String[] transactorArr = transactors.split(",");
		for (String transactor : transactorArr) {
			if (transactor != null && !"".equals(transactor)) {
				result = true;
				break;
			}
		}
		return result;
	}

	/**
	 * 判断指定字符串是否在数组中存在
	 *
	 * @param target 指定字符串
	 * @param srcArr 源数组
	 * @return false默认不存在 true存在
	 */
	public static boolean existInArray(String target, String[] srcArr) {
		boolean bool = false;
		for (String str : srcArr) {
			if (str != null && !"".equals(str) && str.equals(target)) {
				bool = true;
			}
		}
		return bool;
	}

	/**
	 * 判断数组元素是否有重复
	 *
	 * @param Object 指定数组
	 * @return false有重复      true没有重复
	 */
	public static boolean checkArrayIsRepeat(Object... arr) throws Exception {
		Set<Object> isRepeat = new HashSet<Object>();
		//假如没有重复
		boolean flag = true;
		for (Object object : arr) {
			flag = isRepeat.add(object);
			if (!flag) {
				break;
			}
		}
		if (flag) {
			//没有重复
			return true;
		} else {
			//有重复
			return false;
		}
	}

	/**
	 * 用于找出2个数组的里的字符串 差集,  以arr1 为主集,如果arr2里有,arr1里没,那么  arr1没有的字符串就会打印出来
	 *
	 * @param arr1
	 * @param arr2
	 * @return
	 */
	public static List minusString(String[] arr1, String[] arr2) {
		LinkedList<String> list = new LinkedList<String>();
		LinkedList<String> history = new LinkedList<String>();
		String[] firstArr = arr1;
		String[] secondArr = arr2;

		for (String str : firstArr) {
			if (!list.contains(str.trim())) {
				list.add(str.trim());
			}
		}
		for (String str : secondArr) {
			if (list.contains(str.trim())) {
				history.add(str.trim());
				list.remove(str);
			}
		}
		return list;
	}

	//获取插入订单的批次号
	public static String getOrderBachno(String compcode) {
		/** 单条订单批次号生成 **/
		Date now = new Date();
		String time = DateUtil.DateToString(now, DateStyle.YYYY_MM_DD);
		String str = compcode + "_" + time;

		int hour = DateUtil.getHour(now);

		String section = "";

		if (hour >= 0 && hour < 10) {
			section = "1";
		} else if (hour >= 10 && hour < 15) {
			section = "2";
		} else {
			section = "3";
		}

		return str + "-" + section;
	}

	@SuppressWarnings("rawtypes")
	public static List<List> genListToManySubList(List list, int residue) {
		if (ValueUtil.isEmpty(list)) {
			return null;
		}
//		System.out.println("size: "+list.size());
		List<List> result = new ArrayList<List>();
		List subList = new ArrayList();

		if (list.size() > residue) {
			float divisor = (float) list.size() / residue;
			int divisorInt = ValueUtil.toInt(Math.ceil(divisor));
			int beforeIndex = 0;

			for (int i = 1; i <= divisorInt; i++) {
				int index = i * residue;
				if (index <= list.size()) {
					subList = list.subList(beforeIndex, index);
				} else {
					subList = list.subList(beforeIndex, list.size());
				}
				beforeIndex = index;
				result.add(subList);
			}
		}
		return result;
	}

	/**
	 * 拆分List
	 *
	 * @param list
	 * @param size 多少条的时候进行拆分
	 * @return
	 */
	public static int splitListSize(List list, int size) {
		int arrSize = list.size();
		Double count = arrSize / ValueUtil.toDouble(size);
		int result = (int) Math.ceil(count);
		if (result == 0 && arrSize > 0) {
			result = 1;
		}
		return result;
	}

	public static void main(String[] args) {
		/*List<String> list = Arrays.asList("ad","ed","ss");
		System.out.println(getStringCollection(list));
		
		String str="1,3,4,23,3,2,3,2,34,4,34,4548,56,56,5645,5649,7876,7678,98,32,1,3,4,23,3,2,3,2,34,4,34,4548,56,56,5645,5649,7876,7678,98,32,48,45,234,23,323";
		String[] arrayStr =str.split(",");
		List<String> list1=java.util.Arrays.asList(arrayStr);
		List<List> result=genListToManySubList(list1,10);
		System.out.println(result);*/
	}

}
